const seq = require('../db/seq');
const MyDesign = require('../model/my_design');

const myDesign = MyDesign(seq);

class MyDesignController {
    //提交设计
    async add(ctx, next) {
        const user_id = ctx.state.user.user_id;
        const res = ctx.request.body;
        const info = { user_id, ...res };
        try {
            const res2 = await myDesign.create(info);
            ctx.body = {
                code: 0,
                message: '设计提交成功',
                result: res2
            }
        } catch (err) {
            console.log('设计提交失败', err);
            return ctx.body = {
                error: '设计提交失败'
            }
        }
    }

    //获取用户设计数据
    async get(ctx, next) {
        const user_id = ctx.state.user.user_id;
        try {
            const res = await myDesign.findAndCountAll({ where: { user_id } });
            ctx.body = {
                code: 0,
                message: '获取用户设计数据成功',
                result: res
            }
        } catch (err) {
            console.log('获取用户设计数据失败', err);
            return ctx.body = {
                error: '获取用户设计数据失败'
            }
        }

    }
}

module.exports = new MyDesignController();